<?php
if( !defined('BASEPATH') ) exit ( 'No direct script access allowed' );

class Category extends admin_controller
{
	public function __construct()
	{
		parent::__construct();
        
        $this->load->model('product/category_model');
	}

	public function index()
	{
		$sql = $this->db->order_by('id', 'desc')->select('id, name, order, group_category_id, status')->get('category_product')->result_array();

		$this->template->title('Quản lý danh mục')
			->set('body', $sql)
			->set('link_add', URL_ADMIN.'category/add')
			->set('link_edit', URL_ADMIN.'category/edit/')
			->set('link_del', URL_ADMIN.'category/delete/')
            ->set('m_active', '#m_product')
			->build('admin/category/index');
	}
    
    
        
    public function add()
    {

        if($_POST)
        {
            //print_r($_POST);

            $this->form_validation->set_rules('name', 'Tên danh mục', 'required');
            $this->form_validation->set_rules('group_category_id', 'Nhóm danh mục', 'required');

            if($this->form_validation->run() == TRUE)
            {
                
                if(!empty($_FILES['pic']['name']))
                {
                    $config['upload_path'] = './data_uploads/category/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = TRUE;

                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('pic'))
                    {
                        echo $this->upload->display_errors();
                        $image = '';
                    }
                    else
                    {
                        $up = $this->upload->data();
                        $image = 'data_uploads/category/' . $up['file_name'];

                        /*
                        $imageWidth = $up['image_width'];
                        if($imageWidth > 150)
                        {
                            $this->load->library('image_lib');
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $up['full_path'];
                            $config['quality'] = 75;
                            $config['maintain_ratio'] = TRUE;
                            $config['master_dim'] = 'auto';
                            $config['width'] = 150;
                            $config['height'] = 100;

                            $this->image_lib->initialize($config);
                            $this->image_lib->resize();
                        }*/

                    }
                } else {
                    $image = '';
                }    
                
                $arr = array(
                    'name'          => $this->input->post('name'),
                    'slug_name'          => $this->util->alias($this->input->post('name')),
                    'desc'          => $this->input->post('desc'),
                    'group_category_id' => (int)$this->input->post('group_category_id'),
                    'status'            => (int)$this->input->post('status'),
                    'order'             => (int)$this->input->post('order'),
                    'image'              => $image
                );

                if( $this->db->insert('category_product', $arr) )
                {
                    $this->message('info', 'Added successfuly');
                    redirect(URL_ADMIN.'category');
                } else {
                    $this->message('info', 'Added faild');
                    redirect(URL_ADMIN.'category');
                }
            }

        }

         $groups = $this->db->get('group_category')->result_array();
         $this->template->title('Thêm mới danh mục')
            ->set('link_action', URL_ADMIN.'category/add')
            ->set('link_cancel', URL_ADMIN.'category')
            ->set('m_active', '#m_product')
            ->set('groups', $groups)
            ->build('admin/category/add');

    }

    public function edit()
    {
        $id = $this->uri->segment(4);
        $oldPic  = $this->input->post('oldImage');
        if($_POST)
        {

            $this->form_validation->set_rules('name', 'Tên danh mục', 'required');
            $this->form_validation->set_rules('group_category_id', 'Nhóm danh mục', 'required');

            if($this->form_validation->run() == TRUE) {
                
                 if(!empty($_FILES['pic']['name']))
                {
                    $config['upload_path'] = './data_uploads/category/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = TRUE;

                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('pic'))
                    {
                        //echo $this->upload->display_errors();
                        $image = $oldPic;
                    }
                    else
                    {
                        $up = $this->upload->data();
                        $image = 'data_uploads/category/' . $up['file_name'];

                        if($oldPic !='') @unlink($oldPic);
                        
                        /*
                        $imageWidth = $up['image_width'];
                        if($imageWidth > 150)
                        {
                            $this->load->library('image_lib');
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $up['full_path'];
                            $config['quality'] = 75;
                            $config['maintain_ratio'] = TRUE;
                            $config['master_dim'] = 'auto';
                            $config['width'] = 150;
                            $config['height'] = 100;

                            $this->image_lib->initialize($config);
                            $this->image_lib->resize();
                        }*/

                    }
                } else {
                    $image = $oldPic;
                }
                
                $arr = array(
                    'name'          => $this->input->post('name'),
                    'slug_name'          => $this->util->alias($this->input->post('name')),
                    'desc'          => $this->input->post('desc'),
                    'group_category_id' => (int)$this->input->post('group_category_id'),
                    'status'            => (int)$this->input->post('status'),
                    'order'             => (int)$this->input->post('order'),
                    'image'              => $image
                );

                if( $this->db->update('category_product', $arr, array( 'id' => $id )) )
                {
                    $this->message('info', 'Updated successfuly');
                    redirect(URL_ADMIN.'category');
                } else {
                    $this->message('info', 'Updated faild');
                    redirect(URL_ADMIN.'category');
                }
            }

        }

        $items = $this->db->get_where('category_product', array('id' => $id))->row_array();
        $groups = $this->db->get('group_category')->result_array();
        
        $this->template->title('Chỉnh sửa danh mục')
            ->set('link_action', URL_ADMIN.'category/edit/'. $id)
            ->set('link_cancel', URL_ADMIN.'category')
            ->set('items', $items)
            ->set('groups', $groups)
            ->set('m_active', '#m_product')
            ->build('admin/category/edit');
    }

    public function delete()
    {
        $id = $this->uri->segment(4);

        $img = $this->db->select('image')->get_where('category_product', array('id' => $id))->row_array();

        if($img['image'] !=='') @unlink($img['image']);

        if ( $this->db->delete('category_product', array( 'id' => $id )) )
        {
            $this->message('info', 'Deleted successfuly');
            redirect(URL_ADMIN.'category');
        } else {
            $this->message('info', 'Deleted successfuly');
            redirect(URL_ADMIN.'category');
        }

    }
    
    /**
     * 
     *-------------------------
     * Quan ly nhom danh muc
     * ------------------------
     */
    
    
    public function group()
    {
        $sql = $this->db->order_by('id', 'desc')->select('id, name, desc, order, status')->get('group_category')->result_array();

        $this->template->title('Quản lý nhóm danh mục')
            ->set('body', $sql)
            ->set('link_add', URL_ADMIN.'category/add_group')
            ->set('link_edit', URL_ADMIN.'category/edit_group/')
            ->set('link_del', URL_ADMIN.'category/delete_group/')
            ->set('m_active', '#m_product')
            ->build('admin/category/group_index');
    }
    
   
   public function add_group()
    {

        if($_POST)
        {
            //print_r($_POST);

            $this->form_validation->set_rules('name', 'Tên nhóm', 'required');
            //$this->form_validation->set_rules('intro', 'Mô tả ngắn', 'required');

            if($this->form_validation->run() == TRUE)
            {

                $arr = array(
                    'name'          => $this->input->post('name'),
                    'slug_name'     => $this->util->alias($this->input->post('name')),
                    'desc'          => $this->input->post('desc'),
                    'status'            => (int)$this->input->post('status'),
                    'order'             => (int)$this->input->post('order'),
                );

                if( $this->db->insert('group_category', $arr) )
                {
                    $this->message('info', 'Added successfuly');
                    redirect(URL_ADMIN.'category/group');
                } else {
                    $this->message('info', 'Added faild');
                    redirect(URL_ADMIN.'category/group');
                }
            }

        }

        $this->template->title('Thêm mới nhóm danh mục')
            ->set('link_action', URL_ADMIN.'category/add_group')
            ->set('link_cancel', URL_ADMIN.'category/group')
            ->set('m_active', '#m_product')
            ->build('admin/category/add_group');

    }
   
    
    public function edit_group()
    {
        $id = $this->uri->segment(4);

        if($_POST)
        {

            $this->form_validation->set_rules('name', 'Tên nhóm danh mục', 'required');
            //$this->form_validation->set_rules('intro', 'Mô tả ngắn', 'required');

            if($this->form_validation->run() == TRUE) {
                $arr = array(
                    'name'          => $this->input->post('name'),
                    'slug_name'     => $this->util->alias($this->input->post('name')),
                    'desc'          => $this->input->post('desc'),
                    'status'            => (int)$this->input->post('status'),
                    'order'             => (int)$this->input->post('order'),
                );

                if( $this->db->update('group_category', $arr, array( 'id' => $id )) )
                {
                    $this->message('info', 'Updated successfuly');
                    redirect(URL_ADMIN.'category/group');
                } else {
                    $this->message('info', 'Updated faild');
                    redirect(URL_ADMIN.'category/group');
                }
            }

        }

        $items = $this->db->get_where('group_category', array('id' => $id))->row_array();

        $this->template->title('Chỉnh sửa nhóm danh mục')
            ->set('link_action', URL_ADMIN.'category/edit_group/'. $id)
            ->set('link_cancel', URL_ADMIN.'category/group')
            ->set('items', $items)
            ->set('m_active', '#m_product')
            ->build('admin/category/edit_group');
    }
    
    
    public function delete_group()
    {
        $id = $this->uri->segment(4);

        //$img = $this->db->select('image')->get_where('category', array('id' => $id))->row_array();

        //if($img['image'] !=='') @unlink($img['image']);

        if ( $this->db->delete('group_category', array( 'id' => $id )) )
        {
            $this->message('info', 'Deleted successfuly');
            redirect(URL_ADMIN.'category/group');
        } else {
            $this->message('info', 'Deleted successfuly');
            redirect(URL_ADMIN.'category/group');
        }

    }
    
}

/* End file */
/* Local: ./application/controller/admin/page.php */